package com.juc.threadPoolExecutor.threadPoolExecutor1;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Run3_2 {
	 //数量>corePoolSize 并且<=maximumPoolSize的情况
	//队列使用 SynchronousQueue类
	//并且线程数量>corePoolSize 时
	//将其余的任务也放入池中，总数量为8
	//并且线程总数量也没有超过maximumPoolSize值为8
	//由于运行的线程数为8，数量上>corePoolSize为7的值
	//所以keepAliveTime >5时清除空闲线程
	//如果使用SynchronousQueue类则maximumPoolSize参数的作用将有效
	public static void main(String[] args) throws InterruptedException {
		Runnable runnable =  new Runnable() {
			
			@Override
			public void run() {
				try {
					System.out.println(Thread.currentThread().getName() + " run! " +System.currentTimeMillis());
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
		ThreadPoolExecutor executor = new ThreadPoolExecutor(7, 8, 5, TimeUnit.SECONDS,  new SynchronousQueue<Runnable>());
		executor.execute(runnable);//1
		executor.execute(runnable);//2
		executor.execute(runnable);//3
		executor.execute(runnable);//4
		executor.execute(runnable);//5
		executor.execute(runnable);//6
		executor.execute(runnable);//7
		executor.execute(runnable);//8
		Thread.sleep(3000);
		System.out.println("A: " +executor.getCorePoolSize());
		System.out.println("A: " +executor.getPoolSize());
		System.out.println("A: " +executor.getQueue().size());
		Thread.sleep(10000);
		System.out.println("B: " +executor.getCorePoolSize());
		System.out.println("B: " +executor.getPoolSize());
		System.out.println("B: " +executor.getQueue().size());
	}
}
